# -*- coding: utf-8 -*-
# Author: xiantingDeng
# File: execute.py
# Time: 10:20

from common.element.element import WebTools
from common.utils.read_util import OperationYaml
from page.login.handler import LoginHandler


class LoginBiz():
    def __init__(self, driver, log):
        self.__log = log
        self.__driver = WebTools(driver, log)
        self.handler = LoginHandler(driver, log)
        self.explain = OperationYaml("login.yaml")

    def login(self, user, passwor):
        '''
        进行登录操作
        :param user: 账号
        :param passwor: 密码
        :return: 返回是否成功
        '''
        # self.__driver.click_element(self.handler.get_haibao_background(),
        #                             self.explain.get_annotation("haibao_background"))
        # head = self.__driver.get_handles()
        # header = self.__driver.get_handle()
        # for hand in head:
        #     if hand != header:
        #         self.__driver.switching_window(hand, "【孩宝读书会后台】")
        self.__driver.input_vlaue(self.handler.get_user_input(), user, self.explain.get_annotation("user_input"))
        self.__driver.input_vlaue(self.handler.get_password_input(), passwor,
                                  self.explain.get_annotation("password_input"))
        self.__driver.click_element(self.handler.get_login_but(), self.explain.get_annotation("login_but"))
        assert self.__driver.get_element_text(self.handler.get_name_text(),
                                              self.explain.get_annotation("name_text")) == "邓先庭"
        self.__driver.driver_screenshot("登录成功")
        return True

if __name__ == '__main__':
    from common.utils.log_util import logger
    from selenium import webdriver
    driver = webdriver.Chrome(service_args=['--ignore-ssl-errors=true', '--ssl-protocol=TLSv1'])
    driver.get("https://kaiyue-admin.baobaobooks.net/kdk/admin")
    driver.maximize_window()
    driver.implicitly_wait(10)
    LoginBiz(driver, logger()).login("邓先庭", "dxt123456")

